package com.xx.xiawan.net;

import android.content.Context;

import com.dg.acb.utils.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017-09-01.
 */

public class DGJsonRequest  extends DGRequestBase<JSONObject> {
    private DGJsonOJectRequest mJsonObjectRequest;
    public DGJsonRequest(Context context,String url,JSONObject jsonObject){
        super(context,null);
        mJsonObjectRequest = new DGJsonOJectRequest(url,jsonObject,this,this);
        mRequest = mJsonObjectRequest;
    }

    public DGJsonRequest(Context context,int method,String url,JSONObject jsonObject){
        super(context,null);
        mJsonObjectRequest = new DGJsonOJectRequest(method,url,jsonObject,this,this);
        mRequest = mJsonObjectRequest;
    }

    public DGJsonRequest(Context context,String url,JSONObject jsonObject,DGRequestListener listener){
        super(context,listener);
        mJsonObjectRequest = new DGJsonOJectRequest(url,jsonObject,this,this);
        mRequest = mJsonObjectRequest;
    }


    public DGJsonRequest(Context context,int method,String url, JSONObject jsonObject, DGRequestListener listener){
        super(context,listener);
        mJsonObjectRequest = new DGJsonOJectRequest(method,url,jsonObject,this,this);
        mRequest = mJsonObjectRequest;
    }
    @Override
    public void onResponse(JSONObject response) {
        LogUtil.d("result"," response is "+response);
        try {
            int code = response.getInt("code");
            String content = response.getString("content");
            jsonResult.result = content;
            if(code == 200){
                if(mRequestListener != null){
                    mRequestListener.onSuccess(jsonResult);
                }
            }else{
                jsonResult.errorCode = DGJsonResult.REQUEST_ERROR;
                if(mRequestListener != null){
                    mRequestListener.onError(content);
                }
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }
        unLock();
    }

    public void header(String name, String value) {
        mJsonObjectRequest.header(name,value);
    }
}
